﻿Imports SecretaryToolData

Public Class frmPublishers

	Private Sub frmPublishers_Load(sender As Object, e As System.EventArgs) Handles Me.Load
		PopulatePublishers(Nothing, Nothing)
	End Sub

	Private Sub btnEdit_Click(sender As System.Object, e As System.EventArgs) Handles btnEdit.Click
		EditPublisher()
	End Sub

	Private Sub btnNew_Click(sender As System.Object, e As System.EventArgs) Handles btnNew.Click
		Dim frmEditPublisherData As New frmPublisherData

		frmEditPublisherData.ShowDialog()

		PopulatePublishers(Nothing, Nothing)
	End Sub

	Private Sub PopulatePublishers(sender As System.Object, e As System.EventArgs) Handles rdoAllPublishers.CheckedChanged, rdoLocalPublishers.CheckedChanged
		Dim publisherCollection As List(Of VM_PublisherInformation)

		Select Case True
			Case rdoAllPublishers.Checked
				publisherCollection = PublisherData.AllPublishers.OrderBy(Function(x) x.FullName).Select(Function(x) New VM_PublisherInformation(x.PublisherID)).ToList
			Case rdoLocalPublishers.Checked
				publisherCollection = PublisherData.LocalPublishers.OrderBy(Function(x) x.FullName).Select(Function(x) New VM_PublisherInformation(x.PublisherID)).ToList
			Case Else
				publisherCollection = PublisherData.AllPublishers.OrderBy(Function(x) x.FullName).Select(Function(x) New VM_PublisherInformation(x.PublisherID)).ToList
		End Select

		lstPublishers.DisplayMember = Extensions.GetMemberInfo(Function(x As VM_PublisherInformation) x.FullName).Name
		lstPublishers.ValueMember = Extensions.GetMemberInfo(Function(x As VM_PublisherInformation) x.PublisherID).Name
		lstPublishers.DataSource = publisherCollection

	End Sub

	Private Sub EditPublisher()
		Dim frmEditPublisherData As New frmPublisherData(lstPublishers.SelectedValue)

		frmEditPublisherData.Show()

		'lstPublishers.Focus()
	End Sub

	Private Sub lstPublishers_DoubleClick(sender As Object, e As System.EventArgs) Handles lstPublishers.DoubleClick
		EditPublisher()
	End Sub

	Private Sub lstPublishers_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles lstPublishers.KeyDown
		If e.KeyCode = Windows.Forms.Keys.Enter Then
			EditPublisher()
		End If
	End Sub

	Private Sub txtFilter_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtFilter.KeyDown
		If e.KeyCode = Windows.Forms.Keys.Enter Then
			EditPublisher()
			e.Handled = True
		End If
	End Sub

	Private Sub txtFilter_TextChanged(sender As Object, e As System.EventArgs) Handles txtFilter.TextChanged
		lstPublishers.FilterString = txtFilter.Text
	End Sub

	Private Sub lstPublishers_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles lstPublishers.KeyPress
		txtFilter.Text = e.KeyChar
		txtFilter.Focus()
		txtFilter.SelectionStart = txtFilter.TextLength
	End Sub

	Private Sub btnNew_GotFocus(sender As Object, e As System.EventArgs) Handles btnNew.GotFocus
		PopulatePublishers(Nothing, Nothing)
	End Sub
End Class